﻿//This file is part of ILoveHUSTBoard.

//ILoveHUSTBoard is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//ILoveHUSTBoard is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with ILoveHUSTBoard.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataHandler.message
{
    public class MessageProcessor
    {
        MessageBoardDataContext _context;
        public MessageProcessor()
        {
            _context = new MessageBoardDataContext();
        }

        public bool AddMessage(views.MessageInfo messageInfo)
        {
            try
            {
                //构造新留言
                TMessage newMessage = new TMessage() { LoveCount = 0, Message = messageInfo.MessageContent, NickName = messageInfo.NickName, SubmitTime = messageInfo.SubmitTime };
                _context.TMessage.InsertOnSubmit(newMessage);
                _context.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        public views.MessageInfo GetMessage(long messageId)
        {
            try
            {
                var resultList = from message in _context.TMessage
                                 where message.Id == messageId
                                 select message;
                //判断是否查到结果
                if (resultList.Count() <= 0)
                    throw new ArgumentOutOfRangeException("messageId", "未找到留言");
                
                var result = resultList.Single();
                //构造视图类
                var info = new views.MessageInfo() { MessageContent = result.Message, NickName = result.NickName, SubmitTime = result.SubmitTime, LoveCount = result.LoveCount };
                info.SetId(messageId);

                return info;
            }
            catch
            {
                return null;
            }
        }

        public List<TMessage> GetMessages(int pageIndex, int pageItemCount)
        {
            try
            {
                var resultList = from message in _context.TMessage
                                 orderby message.SubmitTime descending
                                 select message;


                var limitedResult = resultList.Skip((pageIndex - 1) * pageItemCount).Take(pageItemCount);

                if (limitedResult.Count() > 0)
                {
                    return limitedResult.ToList();
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

        public List<TMessage> GetTopTenLovedMessage()
        {
            try
            {
                var resultList = from message in _context.TMessage
                                 orderby message.LoveCount descending
                                 select message;


                var limitedResult = resultList.Take(10);

                if (limitedResult.Count() > 0)
                {
                    return limitedResult.ToList();
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
